<?php
/**
 * The name might be misleading. It doesn't compress, but just redirect to
 * compressed version of the requested file.
 *
 * @package Extensions
 * @version @@VERSION@@
 * @subpackage Low-Cal_Vanilla
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

define('LOWCALVANILLA_DIR', dirname(__FILE__));
include(LOWCALVANILLA_DIR.'/../../appg/settings.php');
include(LOWCALVANILLA_DIR.'/../../appg/init_ajax.php');

include_once(LOWCALVANILLA_DIR.'/library/Functions.php');
include_once(LOWCALVANILLA_DIR.'/library/Class.Response.php');

// Get last modification of the browser cached requested file
$MathTime = 0;
if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
	$MathTime = ForceInt($_SERVER['HTTP_IF_NONE_MATCH'], 0);
} elseif (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
	if ($Time = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
		$MathTime = ForceInt($Time, 0);
	}
}

//Set response object
$Response = new LowCalVanilla_Response();

if (!$File = LowCalVanilla_GetFileToServe($Context)) {
	$Response->NotFound();
} else {
	if (!$File->HasChanged($MathTime)) {
		$Response->NotChanged();
	} else {
		// Set headers
		$Response->AppendHeaders($File->GetDetails());
		$Response->AddHeader('Cache-Control', 'private');
		$Response->AddHeader('Pragma', '');
		if ($Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED']) {
			$Response->AddHeader('Vary', 'User-Agent,Accept-Encoding');
		}
		// Set body
		$Response->SetBody($File->GetContent());
		$File->Clear();
		unset($File);
		// Send Body and Headers
		$Response->SentResponse();
	}
}